import json
import pandas as pd
import csv
import json
import pandas as pd
import csv
from openai import OpenAI

client = OpenAI(api_key="sk-cc3e631af6d84a68a33bdac5cdeae4b6", base_url="https://api.deepseek.com/v1")
system_prompt = "你是一个专业的医生，请根据患者的问题进行意图分类，类别有：疾病咨询,药物咨询,科室咨询,手术咨询,检查项目咨询,费用咨询,医保咨询,就医建议,术后恢复咨询,预防措施,住院咨询,手术挂号预约,其他，如果有多个意图，请以#进行分隔，一定要只按照提供的类别进行选择回答，不要回答选择原因和其他内容，只考虑问题提到的意图，不要推理其他的意图"

# openai.api_base = "https://api.deepseek.com/v1"
# openai.api_key = "sk-cc3e631af6d84a68a33bdac5cdeae4b6"

# 病症：定义，病因，临床表现，相关病症，治疗方法，推荐医院，预防，所属科室，禁忌，传染性，治愈率，严重性
# 药物：作用，适用症，价钱，药物禁忌，用法，副作用，成分
# 治疗方案：方法，费用，有效时间，临床意义/检查目的，治疗时间，疗效，恢复时间，正常指标，化验/体检方案，恢复
# 其他：设备用法，多问，养生，整容，两性，对比，无法确定

def get_deepseek(system_prompt,prompt):
    response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": prompt},
    ],
    stream=False)
    
    return response.choices[0].message.content


def process_csv_file(file_path):
  # Read the CSV file
  df = pd.read_csv(file_path)
  
  # Iterate over each row in the CSV file
  try:
    for index, row in df.iterrows():
      # Get the query value from the 'query' column
      query = row['query']
      intent = row['intent']
    #   print(intent)
      if str(intent) != 'nan':
        continue
      print(query)
      
      # Call the API with the query value and get the response
      response = get_deepseek(system_prompt,query)
      
      df.at[index, 'intent'] = response
    
  # Save the modified DataFrame back to the CSV file
    df.to_csv(file_path, index=False, encoding='utf-8-sig')
  except Exception as e:
    print(e)
    df.to_csv(file_path, index=False, encoding='utf-8-sig')
    




csv_file_path = "./sdataset/s1/1妇科.csv"

process_csv_file(csv_file_path)